/**
 * 楼栋产品选择js
 */
var selBanProduct= {
	curPage:1,
	pageSize:6,
	dataCache:null,
	
	startEvent:function(){
		//获取楼栋
		var url = 'common/ComBanBasicCtrl/getBanBasicList';
		var cb = function(data){
			if(data.resultCode==1){
				var option='<option value="">请选择</option>';
				for(var i=0;i<data.result.length;i++){
					var singleObj=data.result[i];
					option=option+'<option value="'+singleObj.id+'">'+singleObj.name+'</option>';
				}
				$('#searchModForm select[name="banId"]').append(option);
			}
		};
		sendApiNoLoad(url, {}, cb, 'POST');
		
		//获取户型
		var url = 'common/ComHouseTypeCtrl/getHouseTypeList';
		var cb = function(data){
			if(data.resultCode==1){
				var option='<option value="">请选择</option>';
				for(var i=0;i<data.result.length;i++){
					var singleObj=data.result[i];
					option=option+'<option value="'+singleObj.id+'">'+singleObj.name+'</option>';
				}
				$('#searchModForm select[name="houserTypeId"]').append(option);
			}
		};
		sendApiNoLoad(url, {}, cb, 'POST');
		
		//按回车键登录事件
		document.onkeydown = function(e){ 
    		var ev = document.all ? window.event : e;
    		if(ev.keyCode==13) {
    			selBanProduct.curPage=1;
    			selBanProduct.setPageChange(null);
    			selBanProduct.search();
     		}
		};
	},
	
	bindEvent:function(){	
		//查询
		$('#searchData').click(function(){
			selBanProduct.curPage=1;
			selBanProduct.setPageChange(null);
			selBanProduct.search();
		});
	},
	
	//设置分页
	setPageChange:function(delCurPage){
		var url = 'common/BanProductCtrl/getBanProductList';
		var banId=$.trim($('#searchModForm select[name="banId"]').val());
		var houserTypeId=$.trim($('#searchModForm select[name="houserTypeId"]').val());
		var params={
			'curPage':1,
			'pageSize':99999,
			'banId':banId,
			'houserTypeId':houserTypeId
		};
		var cb = function(data) {
			if (data.resultCode == 1) {
				var totalCount = data.size;
				var pageTotal = (totalCount / selBanProduct.pageSize > 0) ? Math.ceil(totalCount/ selBanProduct.pageSize) : 1;
				pageHelper.init(pageTotal,totalCount,selBanProduct.search,delCurPage);  //翻页事件
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	//查询楼栋产品
	search:function(pageIdx){
		var url = 'common/BanProductCtrl/getBanProductList';
		if (typeof(pageIdx) != "undefined"){ 
			selBanProduct.curPage=pageIdx;
		}
		var banId=$.trim($('#searchModForm select[name="banId"]').val());
		var houserTypeId=$.trim($('#searchModForm select[name="houserTypeId"]').val());
		var params={
			'curPage':selBanProduct.curPage,
			'pageSize':selBanProduct.pageSize,
			'banId':banId,
			'houserTypeId':houserTypeId
		};
		var cb = function(data) {
			if (data.resultCode == 1) {
				$('#tableList tbody').empty();
				var tr='';
				if(data.result!=null&&data.result.length>0){
					selBanProduct.dataCache=data.result;
					var checkedId=art.dialog.data('checkedId');
					for(var i=0;i<data.result.length;i++){
						var singleObj=data.result[i];
						tr += '<tr><td>'
							+'<input type="radio" name="id" value="'+singleObj.id+'" '+(singleObj.id==checkedId?'checked="checked"':'')+'/>'
							+ (i+1)
							+ '</td><td>'
							+ singleObj.banBasic.name
							+ '</td><td>'
							+ singleObj.houseType.name
							+ '</td><td>'
							+ jsUtils.getDictionaryTxtByType('proType', singleObj.proType)
							+ '</td><td>'
							+ jsUtils.getDictionaryTxtByType('proStatus', singleObj.proStatus)
							+ '</td><td>'
							+ (singleObj.direction==null?'':singleObj.direction)
							+ '</td><td>'
							+ (singleObj.sellArea==null?'':singleObj.sellArea)
							+ '</td></tr>';
					}
				}else{
					tr='<tr><td colspan="8" style="text-align: center;color: red;">暂无数据</td></tr>';
				}
				$('#tableList tbody').html(tr);
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
	},
	
	init : function() {
		selBanProduct.setPageChange(null);
		selBanProduct.search();
		selBanProduct.startEvent();
		selBanProduct.bindEvent();
	}
};

$(document).ready(function() {
	selBanProduct.init();
});
function getSelData(){
	var checkedLen=$('#tableList').find('input[name="id"]:checked').length;
	if(checkedLen==0) { 
		alert('至少选择一个产品');
		return false;
	}
	
	var checkId=$('#tableList').find('input[name="id"]:checked').val();
	var ret=[];
	for(var i=0;i<selBanProduct.dataCache.length;i++){
		if(selBanProduct.dataCache[i].id==checkId){
			ret[0]=checkId;
		}
	}
	return ret;
}

